﻿var commandType;

function dal() {
    this.commandType = (set.CommandType == 'SQL' ? 'Text' : 'StoredProcedure');

    var codes = new Array();

    // 复制DBUtility文件夹
    codes.push({
        type: 'copy',
        isfolder: true,
        source: '{0}\\DBUtility'.format(set.VSVersion),
        target: 'DBUtility'
    });

    // 复制DAL文件夹
    codes.push({
        type: 'copy',
        isfolder: true,
        source: '{0}\\{1}'.format(set.VSVersion, set.DALFrame),
        target: set.DALFrame
    });

    // 生成DALHelper.cs
    codes.push(dalHelper());

    // 逐表生成
    for (var i = 0; i < db.Selects.length; i++)
    {
        var table = db.Selects[i];
        var daoClassName = getDalClassName(table.Name);
        codes.push(tableInternalDal(table, daoClassName));
        codes.push(tableEditableDal(table, daoClassName));
    }

    return codes;
}

function dalHelper() {
    var NewLine = '\n';
    var temp = '';
    temp += '//如果要从UI中设置数据库，请取消注释下一行，如果从配置中读取，请注释下一行' + NewLine;
    temp += '//#define UISET' + NewLine;
    temp += NewLine;
    temp += 'using System;' + NewLine;
    temp += 'using System.Configuration;' + NewLine;
    temp += 'using System.Data;' + NewLine;
    temp += 'using System.Data.Common;' + NewLine;
    temp += 'using DBUtility;' + NewLine;
    temp += NewLine;
    temp += 'namespace ' + getNamespace(set.DALFrame) + NewLine;
    temp += '{' + NewLine;
    temp += '    public class DALHelper' + NewLine;
    temp += '    {' + NewLine;
    temp += '#if UISET' + NewLine;
    temp += '        public static DbHelper {0};'.format(set.DBHelperName) + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 设置数据库连接' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        /// <param name="dbType">不区分大小写，可选值：MySql、OleDb、Oracle、SQLite、SqlServer, Ase, DB2,PostgreSql</param>' + NewLine;
    temp += '        /// <param name="connectionString">数据库连接字符串</param>' + NewLine;
    temp += '        public static void SetHelper(string dbType, string connectionString)' + NewLine;
    temp += '        {' + NewLine;
    temp += '            {0} = DbHelper.Create(dbType);'.format(set.DBHelperName) + NewLine;
    temp += '            {0}.ConnectionString = connectionString;'.format(set.DBHelperName) + NewLine;
    temp += '        }' + NewLine;
    temp += '#else' + NewLine;
    temp += '        public static DbHelper {0} = GetHelper("DB");'.format(set.DBHelperName) + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 从Web.config从读取数据库的连接以及数据库类型' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        private static DbHelper GetHelper(string connectionStringName)' + NewLine;
    temp += '        {' + NewLine;
    temp += '            ConnectionStringSettings connectionSetting = ConfigurationManager.ConnectionStrings[connectionStringName];' + NewLine;
    temp += '            string className = connectionSetting.ProviderName;' + NewLine;
    temp += '            DbHelper db = DbHelper.Create(className);' + NewLine;
    temp += '            db.ConnectionString = connectionSetting.ConnectionString;' + NewLine;
    temp += '            return db;' + NewLine;
    temp += '        }' + NewLine;
    temp += '#endif' + NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 对IDataReader读取依次执行事件' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static void ExecuteReaderAction(IDataReader dr, Action<IDataReader> readAction)' + NewLine;
    temp += '        {' + NewLine;
    temp += '            while (dr.Read())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                if (readAction != null) readAction(dr);' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 对IDataReader读取依次执行事件' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static void ExecuteReaderAction(IDataReader dr, int first, int count, Action<IDataReader> readAction)' + NewLine;
    temp += '        {' + NewLine;
    temp += '            for (int i = 0; i < first; i++)' + NewLine;
    temp += '            {' + NewLine;
    temp += '                if (!dr.Read()) return;' + NewLine;
    temp += '            }' + NewLine;
    temp += NewLine;
    temp += '            int resultsFetched = 0;' + NewLine;
    temp += '            while (resultsFetched < count && dr.Read())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                if (readAction != null) readAction(dr);' + NewLine;
    temp += '                resultsFetched++;' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += '    }' + NewLine;
    temp += '}' + NewLine;

    return {
        type: 'code',
        title: 'DALHelper',
        path: '{0}\\DALHelper.cs'.format(set.DALFrame),
        code: temp
    };
}

function tableEditableDal(table, daoClassName) {
    var NewLine = '\n';
    var temp = '';
    temp += 'using System;' + NewLine;
    temp += 'using System.Collections.Generic;' + NewLine;
    temp += 'using System.Data;' + NewLine;
    temp += 'using System.Data.Common;' + NewLine;
    temp += 'using System.Text;' + NewLine;
    temp += 'using System.Linq;' + NewLine;
    temp += 'using DBUtility;' + NewLine;
    temp += 'using NHibernate.Linq;' + NewLine;
    temp += 'using NHibernate;' + NewLine;
    temp += 'using NHibernate.Criterion;' + NewLine;
    temp += NewLine;
    temp += 'namespace {0}'.format(getNamespace(set.DALFrame, set)) + NewLine;
    temp += '{' + NewLine;
    temp += '    /// <summary>' + NewLine;
    temp += '    /// 数据访问类 ' + table.Descn + NewLine;
    temp += '    /// <summary>' + NewLine;
    temp += '    public partial class {0}'.format(daoClassName);
    temp += NewLine;
    temp += '    {' + NewLine;
    temp += '    }' + NewLine;
    temp += '}';

    return {
        type: 'code',
        title: 'DAL-editable',
        path: '{0}\\editable\\{1}.cs'.format(set.DALFrame, table.Name),
        code: temp
    };
}

function tableInternalDal(table, daoClassName) {
    var NewLine = '\n';
    var temp = '';
    var allFields = table.Fields;
    var condFields = table.CondFields;
    var uncondFields = table.UncondFields;
    var hasCompositeKey = condFields.length > 1;//表是否具有复合主键
    var modelClassNameWithoutNamespace = getModelClassName(table.Name); //数据模型类的名称（不含命名空间）
    var modelClassNameWithNamespace = getNamespace('Model') + '.' + modelClassNameWithoutNamespace;//数据模型类的名称（包含命名空间）
    var modelIDClassNameWithoutNamespace = modelClassNameWithoutNamespace + "_ID"; //具有复合主键的数据模型类的标识类的名称（不含命名空间）
    var modelIDClassNameWithNamespace = getNamespace('Model') + '.' + modelIDClassNameWithoutNamespace;//具有复合主键的数据模型类的标识类的名称（包含命名空间）
    var idPropertyName = hasCompositeKey ? modelIDClassNameWithoutNamespace : condFields[0].Name;//标识属性的名称
    var idPropertyCSType = hasCompositeKey ? modelIDClassNameWithNamespace : csType[condFields[0].DbType];//标识属性的C#类型
    temp += 'using System;' + NewLine;
    temp += 'using System.Collections.Generic;' + NewLine;
    temp += 'using System.Data;' + NewLine;
    temp += 'using System.Data.Common;' + NewLine;
    temp += 'using System.Text;' + NewLine;
    temp += 'using System.Linq;' + NewLine;
    temp += 'using DBUtility;' + NewLine;
    temp += 'using NHibernate.Linq;' + NewLine;
    temp += 'using NHibernate;' + NewLine;
    temp += 'using NHibernate.Criterion;' + NewLine;
    temp += NewLine;
    temp += 'namespace {0}'.format(getNamespace(set.DALFrame)) + NewLine;
    temp += '{' + NewLine;
    temp += '    /// <summary>' + NewLine;
    temp += '    /// 数据访问类 {0},{1} ，请勿修改此文件，以方便表字段更改时重新生成覆盖'.format(daoClassName, table.Descn) + NewLine;
    temp += '    /// </summary>' + NewLine;
    temp += '    public partial class {0} : DALHelper'.format(daoClassName);
    if (set.SlnFrame == 'Factory') temp += ', {0}.I{1}'.format(getNamespace('IDAL', set), table.Name);
    temp += NewLine;
    temp += '    {' + NewLine;
    temp += '        #region 私有构造函数，以避免创建该类的实例' + NewLine;
    temp += '        private {0}() { }'.format(daoClassName) + NewLine;
    temp += '        #endregion' + NewLine;
    temp += NewLine;
    temp += '        #region 公共方法' + NewLine;
    temp += NewLine;
    temp += '        #region 查询' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 查询所有数据列表' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static IList<{0}> GetAll()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                return session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .List<{0}>();'.format(modelClassNameWithNamespace) + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 根据查询条件数据列表' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static IList<{0}> GetListByFilter(params FilterInfo[] filters)'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                return session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .Add(dbHelper.ConvertFilterInfoToCriterion(filters))' + NewLine;
    temp += '                    .List<{0}>();'.format(modelClassNameWithNamespace) + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 根据查询条件数据列表' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        /// <param name="filter">查询条件</param>' + NewLine;
    temp += '        /// <param name="orderby">排序字段列表</param>' + NewLine;
    temp += '        /// <returns>查询结果</returns>' + NewLine;
    temp += '        public static IList<{0}> GetListByFilter(IFilterInfo filter, params string[] orderby)'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                var criteria = session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .Add(dbHelper.ConvertFilterInfoToCriterion(filter));' + NewLine;
    temp += '                if (orderby != null && orderby.Length > 0)' + NewLine;
    temp += '                {' + NewLine;
    temp += '                    foreach (var order in orderby)' + NewLine;
    temp += '                    {' + NewLine;
    temp += '                        criteria = criteria.AddOrder(new NHibernate.Criterion.Order(order, true));' + NewLine;
    temp += '                    }' + NewLine;
    temp += '                }' + NewLine;
    temp += '                return criteria.List<{0}>();'.format(modelClassNameWithNamespace) + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 根据数据编号查询数据' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static {0} GetModel({1})'.format(modelClassNameWithNamespace, getCondFieldParameters()) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                return session.Get<{0}>({1});'.format(modelClassNameWithNamespace, getId()) + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    if (hasCompositeKey)
    {
        temp += '        /// <summary>' + NewLine;
        temp += '        /// 根据主键查询数据' + NewLine;
        temp += '        /// </summary>' + NewLine;
        temp += '        /// <param name="id">代表主键的标识对象</param>' + NewLine;
        temp += '        public static {0} GetModel({1} id)'.format(modelClassNameWithNamespace, modelIDClassNameWithNamespace) + NewLine;
        temp += '        {' + NewLine;
        temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
        temp += '            {' + NewLine;
        temp += '                return session.Get<{0}>(id);'.format(modelClassNameWithNamespace) + NewLine;
        temp += '            }' + NewLine;
        temp += '        }' + NewLine;
        temp += NewLine;
    }
    temp += '        #endregion 查询' + NewLine;
    temp += NewLine;
    temp += '        #region 插入' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 增加一条数据' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static bool Add({0} model)'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                using (var transation = session.BeginTransaction())' + NewLine;
    temp += '                {' + NewLine;
    temp += '                    session.Save(model);' + NewLine;
    temp += '                    transation.Commit();' + NewLine;
    temp += '                    return true;' + NewLine;
    temp += '                }' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        #endregion 插入' + NewLine;
    temp += NewLine;
    temp += '        #region 更新' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 更新一条数据' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static bool Update({0} model)'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                using (var transation = session.BeginTransaction())' + NewLine;
    temp += '                {' + NewLine;
    temp += '                    session.Update(model);' + NewLine;
    temp += '                    transation.Commit();' + NewLine;
    temp += '                    return true;' + NewLine;
    temp += '                }' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        #endregion 更新' + NewLine;
    temp += NewLine;
    temp += '        #region 删除' + NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 根据主键创建供NHibernate删除数据库记录的临时实体对象' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        /// <returns></returns>' + NewLine;
    temp += '        private static {0} CreateTransientModel({1})'.format(modelClassNameWithNamespace, getCondFieldParameters()) + NewLine;
    temp += '        {' + NewLine;
    temp += '            return new {0}'.format(modelClassNameWithNamespace) + NewLine;
    temp += '            {' + NewLine;
    temp += '                {0} = {1},'.format(idPropertyName, getId()) + NewLine;
    temp += '            };' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    if (hasCompositeKey)
    {
        temp += '        /// <summary>' + NewLine;
        temp += '        /// 根据标识对象创建供NHibernate删除数据库记录的临时实体对象' + NewLine;
        temp += '        /// </summary>' + NewLine;
        temp += '        /// <param name="id">标识对象</param>' + NewLine;
        temp += '        /// <returns></returns>' + NewLine;
        temp += '        private static {0} CreateTransientModel({1} id)'.format(modelClassNameWithNamespace, modelIDClassNameWithNamespace) + NewLine;
        temp += '        {' + NewLine;
        temp += '            return new {0}'.format(modelClassNameWithNamespace) + NewLine;
        temp += '            {' + NewLine;
        temp += '                {0} = id,'.format(modelIDClassNameWithoutNamespace) + NewLine;
        temp += '            };' + NewLine;
        temp += '        }' + NewLine;
        temp += NewLine;
    }
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 删除一条数据' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static bool Delete({0})'.format(getCondFieldParameters()) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                using (var transation = session.BeginTransaction())' + NewLine;
    temp += '                {' + NewLine;
    temp += '                    session.Delete(CreateTransientModel({0}));'.format(condFields.map(function(a){return a.Name.toLowerCase()}).join(', ')) + NewLine;
    temp += '                    transation.Commit();' + NewLine;
    temp += '                    return true;' + NewLine;
    temp += '                }' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    if (hasCompositeKey) {
        temp += '        /// <summary>' + NewLine;
        temp += '        /// 删除一条数据' + NewLine;
        temp += '        /// </summary>' + NewLine;
        temp += '        public static bool Delete({0} id)'.format(modelIDClassNameWithNamespace) + NewLine;
        temp += '        {' + NewLine;
        temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
        temp += '            {' + NewLine;
        temp += '                using (var transation = session.BeginTransaction())' + NewLine;
        temp += '                {' + NewLine;
        temp += '                    session.Delete(CreateTransientModel(id));' + NewLine;
        temp += '                    transation.Commit();' + NewLine;
        temp += '                    return true;' + NewLine;
        temp += '                }' + NewLine;
        temp += '            }' + NewLine;
        temp += '        }' + NewLine;
        temp += NewLine;
    }
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 删除多条数据' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static bool Delete({0}[] ids, out string errMsg)'.format(idPropertyCSType) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                using (var transation = session.BeginTransaction())' + NewLine;
    temp += '                {' + NewLine;
    temp += '                    foreach ({0} id in ids)'.format(idPropertyCSType) + NewLine;
    temp += '                    {' + NewLine;
    temp += '                        session.Delete(CreateTransientModel(id));' + NewLine;
    temp += '                    }' + NewLine;
    temp += '                    transation.Commit();' + NewLine;
    temp += '                    errMsg = "";' + NewLine;
    temp += '                    return true;' + NewLine;
    temp += '                }' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 删除所有数据' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static void DeleteAll()' + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                using (var transation = session.BeginTransaction())' + NewLine;
    temp += '                {' + NewLine;
    temp += '                    session.Delete("From {0}");'.format(modelClassNameWithoutNamespace) + NewLine;
    temp += '                    transation.Commit();' + NewLine;
    temp += '                }' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        #endregion 删除' + NewLine;
    temp += NewLine;
    temp += '        #region 是否已经存在' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 是否存在该记录' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static bool IsExist({0} id)'.format(idPropertyCSType) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                return session.Query<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .Where(x => x.{0} == id)'.format(idPropertyName) + NewLine;
    temp += '                    .Select(x => x.{0})'.format(idPropertyName) + NewLine;
    temp += '                    .Any();' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += '        #endregion 是否已经存在' + NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 获取数量' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static int GetCount()' + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                return session.Query<{0}>().Count();'.format(modelClassNameWithNamespace) + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 分页获取泛型数据列表' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static PageList<{0}> GetPageList(PageInfo pi)'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                var criteria = session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .SetFirstResult((pi.PageIndex - 1) * pi.PageSize)' + NewLine;
    temp += '                    .SetMaxResults(pi.PageSize)' + NewLine;
    temp += '                    .Future<{0}>();'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                pi.RecordCount = session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .SetProjection(Projections.Count(Projections.Property("{0}")))'.format(idPropertyName) + NewLine;
    temp += '                    .FutureValue<int>().Value;' + NewLine;
    temp += '                pi.Compute();' + NewLine;
    temp += '                var pl = new PageList<{0}>(pi);'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                pl.List = criteria.ToList();' + NewLine;
    temp += '                return pl;' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += NewLine;
    temp += NewLine;
    temp += '        /// <summary>' + NewLine;
    temp += '        /// 分页获取泛型数据列表（带查询条件）' + NewLine;
    temp += '        /// </summary>' + NewLine;
    temp += '        public static PageList<{0}> GetPageListByFilter(PageInfo pi, params FilterInfo[] filters)'.format(modelClassNameWithNamespace) + NewLine;
    temp += '        {' + NewLine;
    temp += '            using (var session = dbHelper.SessionFactory.OpenSession())' + NewLine;
    temp += '            {' + NewLine;
    temp += '                var filterCriterion = dbHelper.ConvertFilterInfoToCriterion(filters);' + NewLine;
    temp += '                var criteria = session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .Add(filterCriterion)' + NewLine;
    temp += '                    .SetFirstResult((pi.PageIndex - 1) * pi.PageSize)' + NewLine;
    temp += '                    .SetMaxResults(pi.PageSize)' + NewLine;
    temp += '                    .Future<{0}>();'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                pi.RecordCount = session.CreateCriteria<{0}>()'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                    .Add(filterCriterion)' + NewLine;
    temp += '                    .SetProjection(Projections.Count(Projections.Property("{0}")))'.format(idPropertyName) + NewLine;
    temp += '                    .FutureValue<int>().Value;' + NewLine;
    temp += '                pi.Compute();' + NewLine;
    temp += '                var pl = new PageList<{0}>(pi);'.format(modelClassNameWithNamespace) + NewLine;
    temp += '                pl.List = criteria.ToList();' + NewLine;
    temp += '                return pl;' + NewLine;
    temp += '            }' + NewLine;
    temp += '        }' + NewLine;
    temp += '        #endregion 公共方法' + NewLine;
    temp += '    }' + NewLine;
    temp += '}' + NewLine;

    function getId()
    {
        if(table.CondFields.length > 1)
        {
            var lowercaseFieldNames = new Array();
            for (var i = 0; i < table.CondFields.length; i++) {
                lowercaseFieldNames.push(table.CondFields[i].Name.toLowerCase());
            }
            return 'new {0}({1})'.format(modelIDClassNameWithNamespace, lowercaseFieldNames.join(' ,'));
        }
        else
        {
            return table.CondFields[0].Name.toLowerCase();
        }
    }

    function getCondFieldParameters() {
        var parameters = '';
        for (var i = 0; i < table.CondFields.length; i++) {
            var field = table.CondFields[i];
            if (i != table.CondFields.length - 1)
                parameters += '{0} {1}, '.format(csType[field.DbType], field.Name.toLowerCase());
            else
                parameters += '{0} {1}'.format(csType[field.DbType], field.Name.toLowerCase());
        }
        return parameters;
    }
    return {
        type: 'code',
        title: 'DAL-internal',
        path: '{0}\\internal\\{1}.cs'.format(set.DALFrame, daoClassName),
        code: temp
    };
}
